<?php

namespace think\seecms\command;

use seecms\See;
use seecms\SeePublish;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Console;

class Publish extends Command
{
    protected $help = '
    php think seecms:publish                            发布seecms
';

    protected function configure()
    {
        // 指令配置
        $this->setName('seecms:publish')
            ->addArgument('name', Argument::OPTIONAL, '目录 默认admin')
            ->addOption('silent', 's', Option::VALUE_NONE, '是否静默输出')
            ->setHelp($this->help);
    }

    protected function execute(Input $input, Output $output)
    {

        $name = $input->getArgument('name');
        $silent = $input->getOption('silent');

        $name = $name ?: 'admin';
        if (!preg_match('/^[a-zA-Z]\w+/', $name)) {
            $output->error('目录名称错误');
            exit(1);
        }

        $warning = function ($message) use ($silent) {
            if (!$silent) {
                \magein\utils\Console::warning($message);
            }
        };

        \magein\utils\Console::eol();
        \magein\utils\Console::blue('Step 1 : 初始化目录,文件,配置文件');
        \magein\utils\Console::eol();

        /**
         * @param mixed ...$_
         * @return string
         */
        $app_dir = function (...$_) use ($name) {
            $dir = '';
            foreach ($_ as $item) {
                if (is_string($item)) {
                    $dir = '/' . trim($item, '/');
                }
            }

            $dir = app_path($name . $dir);

            $dir = rtrim($dir, '\\');

            return rtrim($dir, '/');
        };

        $directories = [
            $app_dir('config'),
            $app_dir('controller'),
            $app_dir('lang'),
            $app_dir('route'),
            $app_dir('view'),
        ];

        $origin_file = function ($name) {
            return __DIR__ . '/../../' . $name;
        };


        // 复制配置文件
        $config = root_path() . '/config/seecms.php';
        if (!is_file($config)) {
            copy($origin_file('config/seecms.php'), $config);
            \magein\utils\Console::green('创建config/seecms.php配置文件完成');
        } else {
            $warning('config/seecms.php配置文件已经存在');
        }

        $files = [
            $app_dir('route') . '/seecms.php' => $origin_file('route/seecms.php'),
            $app_dir('lang') . '/en-us.php' => $origin_file('lang/en-us.php'),
            $app_dir('lang') . '/zh-cn.php' => $origin_file('lang/zh-cn.php'),
            $app_dir() . '/middleware.php' => $origin_file('admin/middleware.php'),
            $app_dir() . '/provider.php' => $origin_file('admin/provider.php'),
        ];

        foreach ($directories as $item) {
            if (!is_dir($item)) {
                mkdir($item, 0755, true);
                if (is_dir($item)) {
                    \magein\utils\Console::success('创建 ' . $item . '目录完成');
                }
            } else {
                $warning('跳过 ' . $item);
            }
        }

        foreach ($files as $file => $origin) {
            if (!is_file($file)) {
                copy($origin, $file);
                if (is_file($file)) {
                    \magein\utils\Console::success('创建' . $item . '文件完成');
                }
            } else {
                $warning('跳过 ' . $file);
            }
        }

        $database = $this->app->env->get('database.database');
        if ($database) {

            \magein\utils\Console::eol();
            \magein\utils\Console::blue('Step 2 : 初始化静态资源文件,数据库文件');
            \magein\utils\Console::eol();

            $publish = new SeePublish();
            $publish->silent($silent);
            $publish->init();
            $publish->module();

            Console::call('see:database', ['run']);
        } else {
            \magein\utils\Console::eol();
            \magein\utils\Console::error('数据库参数尚未配置');
            \magein\utils\Console::eol();
        }

        \magein\utils\Console::eol();
        \magein\utils\Console::blue('Step 3 : 生成前端语言包文件');
        \magein\utils\Console::eol();
        $support = See::config()->lang->support();
        foreach ($support as $key => $item) {
            make_lang_json($key);
            \magein\utils\Console::green('  语言包 : ' . $item);
        }

        $output->writeln('');
        $output->writeln('********************************************************');
        $output->writeln('*******************think-seecms安装完成*****************');
        $output->writeln('********************************************************');
    }
}